package com.jingmi.jingmiliving.commodity.service.impl;


import com.jingmi.jingmiliving.commodity.dao.AttrAttrgroupRelationDao;
import com.jingmi.jingmiliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.jingmi.jingmiliving.commodity.entity.AttrgroupEntity;
import com.jingmi.jingmiliving.commodity.service.AttrAttrgroupRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingmi.common.utils.PageUtils;
import com.jingmi.common.utils.Query;

import com.jingmi.jingmiliving.commodity.dao.AttrDao;
import com.jingmi.jingmiliving.commodity.entity.AttrEntity;
import com.jingmi.jingmiliving.commodity.service.AttrService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Resource
    AttrAttrgroupRelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void saveAll(AttrEntity attrEntity) {
        this.save(attrEntity);
        //应为mybatis会自动回填主键
        //String attrName = attrEntity.getAttrName();
        //List<AttrEntity> attrName1 = this.list(new QueryWrapper<AttrEntity>().eq("attr_name", attrName));
        if (attrEntity.getAttrType() == 1 && attrEntity.getAttrGroupId() != null) {
            AttrAttrgroupRelationEntity relationEntity = new
                    AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attrEntity.getAttrGroupId());
            relationEntity.setAttrId(attrEntity.getAttrId());
            System.out.println("relationEntity===>"+relationEntity);
            relationDao.insert(relationEntity);
        }

    }
    //销售属性查询
    @Override
    public PageUtils querySaleAttrPage(Map<String, Object> params,Long saleAttrId) {

        Object object = params.get("attrType");
        Integer attrType = Integer.parseInt((String) object);

        System.out.println("attrType====>"+attrType+"attrClass==>"+attrType.getClass());
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        //判断类型是否为销售属性 是就根据id和name查询返回
        if (attrType.equals(0)){
            System.out.println("进入到attrType==0");
            String key = (String)params.get("key");
            //查询条件 //params不为空
            if (StringUtils.isNotBlank(key)){
                wrapper.eq("attr_type",0).and((obj)->{
                    obj.eq("attr_id", key).or().like("attr_name", key);
                });
            }else {
                wrapper.eq("attr_type",0);
            }
            IPage<AttrEntity> page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    wrapper
            );
            System.out.println("==============条件查询完毕返回数据====================");
            return new PageUtils(page);
        }else {
            System.out.println("进入到attrType!=0");
            //不行就返回默认值
            wrapper.eq("attr_type",0);
            IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params),
                    wrapper);
            System.out.println("==============没有条件查询完毕返回数据====================");
            return new PageUtils(page);
        }




    }
    //基础的属性查询
    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params,Long baseAttrId) {
        Object object = params.get("attrType");
        Integer attrType = Integer.parseInt((String) object);

        System.out.println("attrType====>"+attrType+"attrClass==>"+attrType.getClass());
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        //判断类型是否为base 是就根据id和name查询返回
        if (attrType.equals(1)){
            System.out.println("条件判断");
            String key = (String)params.get("key");
            //查询条件 //params不为空
            if (StringUtils.isNotBlank(key)){
                wrapper.eq("attr_type",1).and((obj)->{
                    obj.eq("attr_id", key).or().like("attr_name", key);
                });

            }else {
                wrapper.eq("attr_type",1);
            }

            IPage<AttrEntity> page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        }else {
            wrapper.eq("attr_type",1);
            System.out.println("进入到attrType!=0");
            //不行就返回默认值
            IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params),
                    wrapper);

            return new PageUtils(page);
        }
    }
    @Transactional
    @Override
    public void baseAttrDelete(Long[] attrIds) {
        System.out.println("attrids===>" +attrIds[0]);
        System.out.println("找关系表");
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationService.selectByAttrId(attrIds);
        System.out.println(attrAttrgroupRelationEntities);
        System.out.println("找完");
        ArrayList<Long> ids = new ArrayList<>();
        for (AttrAttrgroupRelationEntity attrAttrgroupRelationEntity : attrAttrgroupRelationEntities) {
            Long id = attrAttrgroupRelationEntity.getId();
            ids.add(id);
        }
        System.out.println("ids==>"+ids);
        attrAttrgroupRelationService.removeByIds(ids);
        this.removeByIds(Arrays.asList(attrIds));

    }

    @Override
    public List<AttrEntity> queryBaseByAttrIdPage(List<Long> attrId) {
        //in 会循环list eq不会
        List<AttrEntity> attrId1 = this.list(new QueryWrapper<AttrEntity>().in("attr_id",attrId));
        return attrId1;
    }

    @Override
    public  List<AttrEntity> queryBaseAttrPageByCategoryId(Long categoryId) {

        List<AttrEntity> category = this.list(new QueryWrapper<AttrEntity>().eq("category_id", categoryId));
        return category;
    }


}